package com.koron.order.overhaul.controller;

import com.alibaba.fastjson.JSON;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.annocation.CasUser;
import com.koron.order.common.bean.po.OrderSignBean;
import com.koron.order.overhaul.bean.vo.*;
import com.koron.order.overhaul.service.OverhaulOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author twcao
 * @title: OverhaulOrderExecuteController
 * @projectName GDH_EAM_V2
 * @description: 大修工单-执行中
 * @date 2021/10/219:08
 */
@Slf4j
@Api(tags = "大修工单-执行中")
@RestController
@RequestMapping("/overhaul/execute")
public class OverhaulOrderExecuteController {

    @Autowired
    private OverhaulOrderService overhaulOrderService;

    @ApiOperation(value = "执行中(status = 3)，保存执行信息，返回执行信息id")
    @PostMapping("/saveExecuteInfo")
    public Response<String> saveExecuteInfo(@CasUser @ApiIgnore EamUser user, @RequestBody OrderExecuteVO vo) {
        log.info("执行中-新增或修改，vo = {}", JSON.toJSONString(vo));
        return Response.ok("操作成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "saveExecuteInfo", String.class, vo));
    }

    @ApiOperation(value = "执行中(status = 3)，新增或修改(表单,作业过程)，返回表单id")
    @PostMapping("/saveOrUpdate")
    public Response<OverhaulOrderMajorFormVO> execute(@CasUser @ApiIgnore EamUser user, @RequestBody OverhaulOrderMajorFormVO vo) {
        log.info("执行中-新增或修改(表单,作业过程)，vo = {}", JSON.toJSONString(vo));
        return Response.ok("操作成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "execute", OverhaulOrderMajorFormVO.class, vo));
    }

    @ApiOperation(value = "执行中(status = 3)，作业过程-数据记录变量")
    @PostMapping("/params/saveOrUpdate")
    public Response<Boolean> execute(@CasUser @ApiIgnore EamUser user, @RequestBody OverhaulOrderItemParamsVO vo) {
        log.info("执行中-作业过程-数据记录变量，vo = {}", JSON.toJSONString(vo));
        return Response.ok("操作成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "updateParam", Boolean.class, vo));
    }

    @ApiOperation(value = "执行中(status = 3)，作业过程项-列值修改（含确认状态和备注）")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "作业过程项id", name = "itemId", required = true),
            @ApiImplicitParam(value = "作业过程项列名, 确认状态: key=result, 备注: key=desc, 风险：key=", name = "key", required = true),
            @ApiImplicitParam(value = "作业过程项列值", name = "value", required = true)
    })
    @GetMapping("/{itemId}/{key}/{value}")
    public Response<Boolean> itemValue(@CasUser @ApiIgnore EamUser user,  @PathVariable("itemId") String itemId, @PathVariable("key") String key, @PathVariable("value") String value) {
        log.info("执行中-作业过程项-列值修改（含确认状态和备注），id = {}, key = {}, value = {}", itemId, key, value);
        return Response.ok("操作成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "itemValue", Boolean.class, new ItemValueVO(itemId, key, value)));
    }

    @ApiOperation(value = "执行中(status = 3)，根据工单id查询执行信息")
    @GetMapping("/queryExecuteInfoByOrderId")
    public Response<List<OverhaulOrderMajorVO>> queryExecuteInfoByOrderId(@CasUser @ApiIgnore EamUser user, @RequestParam String orderId) {
        log.info("执行中-根据工单id查询执行信息，orderId = {}", orderId);
        return Response.ok("操作成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "queryExecuteInfoByOrderId", List.class, orderId));
    }

    @ApiOperation(value = "执行中(status = 3)，根据执行id删除执行信息(包含零部件)")
    @DeleteMapping("/deleteExecuteInfoByExecuteId")
    public Response<Boolean> deleteExecuteInfoByExecuteId(@CasUser @ApiIgnore EamUser user, @RequestParam String executeId) {
        log.info("执行中-根据执行id删除执行信息，executeId = {}", executeId);
        return Response.ok("操作成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "deleteExecuteInfoByExecuteId", Boolean.class, executeId));
    }

    @ApiOperation(value = "新增签名")
    @PostMapping("/sign/saveOrUpdate")
    public Response<Boolean> sign(@CasUser @ApiIgnore EamUser user, @RequestBody OrderSignBean signBean) {
        log.info("执行中-新增签名，signBean = {}", JSON.toJSONString(signBean));
        return Response.ok("操作成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "sign", Boolean.class, signBean));
    }

    @ApiOperation(value = "执行中(status = 3)，根据工单id只查询执行信息")
    @GetMapping("/getExecuteInfoByOrderId")
    public Response<List<OverhaulOrderMajorVO>> getExecuteInfoByOrderId(@CasUser @ApiIgnore EamUser user, @RequestParam String orderId) {
        log.info("执行中-getExecuteInfoByOrderId:根据工单id只查询执行信息，orderId = {}", orderId);
        return Response.ok("操作成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "getExecuteInfoByOrderId", List.class, orderId));
    }
}
